-- Instance
-- Created by chengb Aug/4/2016
-- 副本格子

-- 初始化
function initInstance(self)
    self.item:removeAllChildren();

    local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
    if type(dbase) == "table" and type(dbase["need_property"]) == "number" then
        -- 配置了需要道具才出现
        if ItemM.getAmount(ME.user, dbase["need_property"]) <= 0 then
            -- 包裹中没有需求的道具，隐藏副本
            return;
        end
    end

    -- 获取副本id
    local instanceId = self.gridData.instanceId;
    local instanceInfo = DungeonInstanceM.getInstanceInfo(instanceId);
    if not instanceInfo then
        error(string.format("获取不到副本(%s)信息，无法初始化副本建筑。", tostring(instanceId)));
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 如果是勇者斗恶龙的副本
            local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");
            if type(elementDbase["lock_id"]) == 'number' and SkyInstanceM.isInSkyInstance() then
                if not BraveVSDragonM.isBuidingUnlock(elementDbase["lock_id"]) then
                    -- 建筑尚未解锁
                    alert(getLocStr("cannot_arrive"));
                    return;
                end

                -- 如果已经加入龙王，提示城镇被毁灭
                local choose = BraveVSDragonM.getDragonKingJoinChoose();
                if choose == BraveVSDragonM.CHOOSE_TYPE_ACCEPT then
                    alert(getLocStr("dragon_king_destory"));
                    return;
                end

                -- 直接进入副本
                DungeonActionM.go("start_instance", self.gridData:getPos(), instanceId);
                return;
            end

            -- 如果是流浪者帐篷副本
            if DungeonTentM.isTentInstance(instanceId) and
               not self.gridData:canHit() then
                alert(getLocStr("grid_unreachable"));
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 获取格子位置
            local pos = self.gridData:getPos();

            -- 如果是BOSS层的帐篷那么点击直接离开
            local okFunc = nil;
            local okText = nil;
            if DungeonBossM.isInBossLayer() then
                okFunc = function()
                    UIMgr.getCurrentScene():removeFormByName("UIInstanceEnter");
                end
                okText = getLocStr("btn_text_leave");
            end

            -- 打开副本进入界面
            UIMgr.getCurrentScene():removeFormByName("UIInstanceEnter");
            local uiInstanceEnter = UIInstanceEnter.create(pos, instanceId, okFunc, okText);
            UIMgr.getCurrentScene():addForm(uiInstanceEnter);

            -- 音效
            AudioM.playFx("workshop_store");
        end
    end
    self.instanceClick = onClicked;

    local icon = getLevelItemIconPath(instanceInfo["icon"]);
    self.icon = ccui.ImageView:create(icon);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    -- 如果是流浪者帐篷副本，更新刷怪倒计时
    if DungeonTentM.isTentInstance(instanceId) then
        self:updateCountDown();
    end
end
